#!/bin/csh -f
# epc: constrained relaxation script
set USAGE="Synopsis: epc TICOFILE [ options ] "
set N_O=0 #set by us not user
set O_LIST=() # optionally set by user
set SRCDIR="$DECAHOME/SRC"
set EXEC="$DECAHOME/bin/epc_"
set NSTEPS=10
set silent=0 # 1 disables message after each step
set FNAME=
set XGAMMA=0.5
set DRMAX=0.5
set cueff=0
while ( "$*" != "" )
    switch ( $1 )
	case -0:
	    set NSTEPS=0
	breaksw
	case -steps:
	case -s:
	    goto setsteps
	breaksw
setsteps:
	    shift
	    set NSTEPS=$1
	breaksw
	case -ueff:
	    shift
	    set cueff=$1
	breaksw
	case -gamma:
	    shift
	    set XGAMMA=$1
	breaksw
	case -drmax:
	    shift
	    set DRMAX=$1
	breaksw
	case -orb:
	    shift
	    set O_LIST=($O_LIST $1)
	    @ N_O++
	breaksw
	case -q:
	    set silent=1
	breaksw
	case -h:
	    shift
	    if !( $1 == "" ) then
		awk -f $SRCDIR/pophelp.awk $SRCDIR/epc.help $1
	    else
		echo $USAGE
		lsopt $0
	    endif
	    exit
	breaksw
	default:
	    if ( -f $1 ) then 
		set FNAME=$1
#		echo FNAME $FNAME
	    else
		echo "Unknown option or nonexistent file $1"
		echo $USAGE
		exit
	    endif
	breaksw
    endsw
    shift
end
if ( $FNAME == "" ) then
    echo Undefined XYZ filename.
    exit
endif
if !( -f $FNAME ) then
    echo Filename $FNAME does not exist
    exit
endif

#AUTOMATIC SETING OF INPUT_TYPE AND CONSISTENCY CHECKS:
if !( -f $SRCDIR/"tico.awk" ) then
    echo "d04: Missing awk file "$SRCDIR/"tico.awk."
    exit
endif
set FNOK=`awk -f $SRCDIR/"tico.awk" -s $FNAME`  #option -s tell tico to be "silent"
set TINP=$FNOK[1]
if ( $TINP > 1 ) then
    echo "TINP>1: $FNAME is neither TILING nor CONFIG type."
    exit
endif
set ERR=$FNOK[2]
set ERRPO=3
if ( $TINP > 0 ) then
    set ERRPO=4
endif
set ERRMSG=`echo $FNOK[$ERRPO-]`
set TMSG=`echo "TILING CONFIG FLIP_REP"`
if ( $ERR > 0 ) then
    @ ind = $TINP + 1
    echo $FNAME" ("$TMSG[$ind]" type): "$ERRMSG
    exit
endif
set rtvl=`awk 'FNR==3{x=0; if($0~/read_tvl*\=*1/)x=1; print x; exit}' $FNAME`
set ORB_LIST="$N_O $O_LIST"
(echo $NSTEPS $silent $cueff $XGAMMA $DRMAX $TINP $rtvl $ORB_LIST ; echo $FNAME) | $EXEC
##

